﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace RunningCalculator
{
    /// <summary>
    /// Interaction logic for PaceSettingControl.xaml
    /// </summary>
    public partial class PaceSettingControl : UserControl
    {
        public uint PaceData = 240;
        public double LengthData = 5;

        public Action<int> deleteAction = null;
        public Action sectionLengthUpdateAction = null;
        private int controlIndex = -1;
        public PaceSettingControl(int index)
        {
            InitializeComponent();
            controlIndex = index;

            distanceIndexLabel.Content = "第 " + (controlIndex+1).ToString() + " 段配速";
        }

        private void paceDeleteButton_Click(object sender, RoutedEventArgs e)
        {
            if (MessageBox.Show("确定删除"+ "第 " + (controlIndex + 1).ToString() + " 段配速？", "操作确认", MessageBoxButton.OKCancel, MessageBoxImage.Question) == MessageBoxResult.OK)
                deleteAction?.Invoke(controlIndex);
        }

        public void updateActionCallBack(int index)
        {
            if(controlIndex > index)
            {
                --controlIndex;
                distanceIndexLabel.Content = "第 " + (controlIndex + 1).ToString() + " 段配速";
            }
        }

        private void paceTextBox_TextChanged(object sender, TextChangedEventArgs e)
        {
            if (UInt32.TryParse(paceTextBox.Text.Trim(), out uint result))
            {
                if (result > 0)
                {
                    if (result > 2400 * 3600) //100天
                    {
                        MessageBox.Show("滚！叔的玩意不适合你！");
                        paceTextBox.Text = PaceData.ToString();
                    }
                    else
                    {
                        PaceData = result;
                        if (timeLabel != null)
                            timeLabel.Content = "(" + Utility.ConvertSecondsToHourMinuteSecond(PaceData, true) + ")";
                    }
                }
            }
            else
            {
                paceTextBox.Text = PaceData.ToString();
            }
        }

        private void lengthTextBox_TextChanged(object sender, TextChangedEventArgs e)
        {
            if (Double.TryParse(lengthTextBox.Text.Trim(), out double result))
            {
                if (result >= 0)
                {
                    if (result > 10000)
                    {
                        MessageBox.Show("你TM打算去月球？");
                        lengthTextBox.Text = LengthData.ToString();
                    }
                    else
                    {
                        LengthData = result;
                        sectionLengthUpdateAction?.Invoke();
                    }
                }
                else
                {
                    MessageBox.Show("弄个负的，你打算挖洞？", "错误提示", MessageBoxButton.OK, MessageBoxImage.Error);
                    lengthTextBox.Text = LengthData.ToString();
                }
            }
            else
            {
                lengthTextBox.Text = LengthData.ToString();
            }
        }
    }
}
